package com.wangs.order.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangs.order.entity.Order;
import com.wangs.order.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    RestTemplate restTemplate;

    @Override
    @Trace
    @Tag(key = "findAll", value = "returnedObj")
    public List<Order> findAll() {
        return orderMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    public int insertOrder(Order order) {
        return orderMapper.insert(order);
    }

    @Override
//    @Transactional
    @GlobalTransactional
    public Order create(Order order) {
        orderMapper.insert(order);

        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();
        paramMap.add("productId", order.getProductId());

        String msg = restTemplate.postForObject("http://localhost:8071/stock/reduce", paramMap, String.class);

        int a = 1 / 0;

        return order;
    }

    @Override
    @Trace
    @Tags({
            @Tag(key = "param", value = "arg[0]"),
            @Tag(key = "order", value = "returnedObj")
    })
    public Order getById(Integer id) {
        return orderMapper.selectById(id);
    }


}
